package me.hetong.caigou.ui.stage;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import me.hetong.caigou.entity.Chess;
import me.hetong.caigou.entity.View;
import me.hetong.caigou.entity.Qipu;
import me.hetong.caigou.ui.ViewFactory;
import me.hetong.caigou.ui.chessboard.ChessboardForEdit;
import me.hetong.caigou.ui.chessboard.ChessboardWarImage;
import me.hetong.caigou.util.FenUtils;
import me.hetong.caigou.util.FxUtils;

import java.util.List;

import static me.hetong.caigou.Global.*;

/**
 * 编辑当前局面
 */
public class EditJuStage {

    private static View VIEW;
    private static final int gridSize = 40;
    private static ChessboardForEdit chessboard;
    private static AnchorPane root;
    private static boolean editFlag = false;

    /**
     * @param fen 为空是表示是新增棋谱，不为空时表示编辑当前局面
     */
    public static void init(String fen) {

        chessboard = new ChessboardForEdit(gridSize);
        if (StrUtil.isBlank(fen)) {
            editFlag = false;
            chessboard.setChessList(FenUtils.ORIGIN_FEN);
        } else {
            editFlag = true;
            chessboard.setChessList(fen);
        }


        VBox vBox = new VBox();
        vBox.setSpacing(config.getSpace());
        vBox.setAlignment(Pos.CENTER);

        vBox.getChildren().add(createRow1());
        vBox.getChildren().add(createRow6());
        vBox.getChildren().add(createRow5());

        root = createRightBox(vBox);

        VIEW = ViewFactory.build(root);
        VIEW.getStage().show();
    }

    private static Pane createRow6() {
        HBox hBox = new HBox();
        hBox.setSpacing(config.getSpace());
        hBox.setAlignment(Pos.CENTER);

        hBox.getChildren().add(createRow2());
        hBox.getChildren().add(chessboard);
        hBox.getChildren().add(createRow3());

        return hBox;
    }

    private static Pane createRow1() {

        HBox row = FxUtils.createHBox();
        row.setSpacing(config.getSpace());
        row.setAlignment(Pos.TOP_CENTER);

        row.getChildren().add(FxUtils.createLabelPro("秃子", new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                chessboard.setChessList("4k4/9/9/9/9/9/9/9/9/4K4");
            }
        }));

        row.getChildren().add(FxUtils.createLabelPro("有护卫", new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                chessboard.setChessList("2bakab2/9/9/9/9/9/9/9/9/2BAKAB2");
            }
        }));

        row.getChildren().add(FxUtils.createLabelPro("兵强马壮", new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                chessboard.setChessList("");
            }
        }));

        row.getChildren().add(FxUtils.createLabelPro("删除棋子", new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                Chess chess1 = chessboard.getClick1();
                if (chess1 != null) {
                    if (StrUtil.isNotBlank(chess1.getName())) {
                        if (chess1.getK().equalsIgnoreCase("k")) {
                            VIEW.getMessageBean().info("将帅都删除了还玩个毛线啊");
                            return;
                        }
                        FenUtils.popChess(chessboard.getChessList(), chess1.getX(), chess1.getY());
                        chessboard.setChessList(FenUtils.chessListToFen(chessboard.getChessList()));
                        chessboard.setClick1(null);
                        chessboard.draw();
                    } else {
                        VIEW.getMessageBean().info("大胸弟，先点棋子再点删除，懂规矩");
                    }
                } else {
                    VIEW.getMessageBean().info("大胸弟，先点棋子再点删除");
                }
            }
        }));

        row.getChildren().add(FxUtils.createLabelPro("复制FEN", new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                try {
                    ClipboardContent content = new ClipboardContent();
                    content.putString(FenUtils.chessListToFen(chessboard.getChessList()));
                    Clipboard clipboard = Clipboard.getSystemClipboard();
                    clipboard.setContent(content);
                    VIEW.getMessageBean().info("复制成功", content.getString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }));

        return row;
    }

    private static Pane createRow2() {

        VBox vBox = new VBox();
        vBox.setSpacing(config.getSpace());
        vBox.setAlignment(Pos.TOP_CENTER);

        String[] arr = new String[]{"r", "n", "b", "a", "c", "p"};

        for (String s : arr) {
            Label button = new Label();
            vBox.getChildren().add(button);
            createCC(button, s, false);
        }

        return vBox;
    }

    private static Pane createRow3() {

        VBox vBox = new VBox();
        vBox.setSpacing(5);
        vBox.setAlignment(Pos.BOTTOM_CENTER);

        String[] arr = ArrayUtil.reverse(new String[]{"R", "N", "B", "A", "C", "P"});

        for (String s : arr) {
            Label button = new Label();
            vBox.getChildren().add(button);
            createCC(button, s, true);
        }

        return vBox;
    }

    private static void createCC(Label button, String s, boolean red) {
        String url = red ? "image/chessboard/style/default/r" + s + ".png" : "image/chessboard/style/default/b" + s + ".png";
        Image image = new Image(url);
        ImageView imageView = new ImageView(image);
        imageView.setFitWidth(gridSize);
        imageView.setFitHeight(gridSize);
        imageView.setOpacity(0.63);
        button.setGraphic(imageView);

        Chess chess = JSONUtil.toBean(FenUtils.map.get(s), Chess.class);
        clickEvent(chess, button);
    }

    private static void clickEvent(Chess chess, Label button) {
        button.setOnMouseClicked(event -> {

            Chess blank = chessboard.getBlank();
            Chess chess1 = chessboard.getClick1();

            List<Chess> chessList = chessboard.getChessList();

            if (blank != null) {
                chess.setX(blank.getX());
                chess.setY(blank.getY());
            } else if (chess1 != null) {
                FenUtils.popChess(chessList, chess1.getX(), chess1.getY());
                chess.setX(chess1.getX());
                chess.setY(chess1.getY());
            }
            chessList.add(chess);
            chessboard.setChessList(FenUtils.chessListToFen(chessList));
            chessboard.draw();
        });
    }

    private static Pane createRow5() {

        HBox row = FxUtils.createHBox();
        row.setSpacing(config.getSpace());
        row.setAlignment(Pos.TOP_CENTER);

//        row.getChildren().add(FxUtils.createLabelPro("导入局面", new EventHandler<MouseEvent>() {
//            @Override
//            public void handle(MouseEvent event) {
//                EditJuStage.chessboard.setChessList(ChessboardWarImage.chessList);
//                Message.info(root, "导入成功", FenUtils.chessListToFen(ChessboardWarImage.chessList));
//            }
//        }));

        row.getChildren().add(FxUtils.createLabelPro("关闭", new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                VIEW.getStage().close();
            }
        }));

        row.getChildren().add(FxUtils.createLabelPro("确定", new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                qipu = new Qipu();
                ChessboardWarImage.initChessList(FenUtils.chessListToFen(chessboard.getChessList()));
                VIEW.getStage().close();
            }
        }));

        return row;
    }
}